here i will explain how to use my CPU, how it works(surface lvl), and inner workings(surface lvl)
a few things to note about this CPU
there are 4 general purpose registers(denoted by "R_") that you can apoint: "R0", "R1", "R2", "R3".
this CPU has the following CPU cycle: Fetch -> Decode&Execute / Store -> Increment PC / Store -> Wait
each of these are 2 clock cycles ( 1 clock cycle is: clock ON(1), clock OFF(0) )
clock OFF ( 0 ) clock ON ( 1 )
ASSEMBLY
u will need to know the CPU intruction set (aka the operations that my CPU can perform)
and thats about it
here it is in short
the following is a tree of all posibilities and all the assembly intructions converted into binary, its a bit easier to comprehend
00 = ALU
- SUB INTRUCTION
00 = ADD
01 = SUB
10 = INC
11 = DEC
- REGISTER A
00 = R0
01 = R1
10 = R2
11 = R3
- REGISTER B
00 = R0
01 = R1
10 = R2
11 = R3
01 = SAV
- REGISTER A
00 = R0
01 = R1
10 = R2
11 = R3
- MEMORY ADDRESS
0000
....
1111
10 = GET
- REGISTER A
00 = R0
01 = R1
10 = R2
11 = R3
- MEMORY ADDRESS
0000
....
1111
11 = JMP
- SUB INTRUCTION
00 = JMP
01 = JZR
10 = JNG
11 = JOV
- MEMORY ADDRESS
0000
....
1111
if we wanted to write a line of assembly it would look a bit like this:
"ALU SUB R2 R3" and the binary equivalent would be: "11 10 01 00"
so "ALU SUB R2 R3" = "11 10 01 00"
here are a few examples:
Making a Python compiler
the CPU cannot read assembly therefore we need to convert it to binary
the way we do this is through compilers
while manually in ur head turning the assembly into binary is fine, a python compiler is much faster and u can think less
i have made a python compiler specific to my CPU intruction set that will turn the assembly inputed into binary
im not going to explain how the python compiler actually works im just gonna give u the python script and tell you how to use it
code has been copied
you can use the online python website below
after paste-ing the code below you can click run and the program will start
it will ask u to input assembly
all u need to write is one of the options
print("=== python assembly builder for the 8-BIT CPU ===")
assembly = ["---", "---", "---", "---"]
binary = ["--","--","--","--"]
current_options = "'ALU', 'SAV', 'GET', 'JMP'"
for i in range(4):
print("current assembly inputed:\t", assembly, "\ncurrent binary outputed:\t", binary)
print("current options:", current_options)
c = input("Enter ur choice: ")
if i == 0:
if c == "ALU":
assembly[i] = c
binary[3-i] = "00"
current_options = "'ADD', 'SUB', 'INC', 'DEC'"
elif c == "SAV":
assembly[i] = c
binary[3-i] = "01"
current_options = "'R0', 'R1', 'R2', 'R3'"
elif c == "GET":
assembly[i] = c
binary[3-i] = "10"
current_options = "'R0', 'R1', 'R2', 'R3'"
elif c == "JMP":
assembly[i] = c
binary[3-i] = "11"
current_options = "'JMP', 'JZR', 'JNG', 'JOV'"
else: print("Invalid input");break
elif i == 1:
if (c == "ADD" and binary[3] == "00") or (c == "R0" and binary[3] == "01" or binary[3] == "10") or (c == "JMP" and binary[3] == "11"):
assembly[i] = c
binary[3-i] = "00"
elif (c == "SUB" and binary[3] == "00") or (c == "R1" and binary[3] == "01" or binary[3] == "10") or (c == "JZR" and binary[3] == "11"):
assembly[i] = c
binary[3-i] = "01"
elif (c == "INC" and binary[3] == "00") or (c == "R2" and binary[3] == "01" or binary[3] == "10") or (c == "JNG" and binary[3] == "11"):
assembly[i] = c
binary[3-i] = "10"
elif (c == "DEC" and binary[3] == "00") or (c == "R3" and binary[3] == "01" or binary[3] == "10") or (c == "JOV" and binary[3] == "11"):
assembly[i] = c
binary[3-i] = "11"
elif len(c) == 4:
assembly[i] = c
binary[3-i] = "10"
break
else: print("Invalid input");break
if binary[3] == "00":
current_options = "'R0', 'R1', 'R2', 'R3'"
elif binary[3] == "01" or binary[3] == "10" or binary[3] == "11":
current_options = "'0000', '0001', '....', '1111'"
elif i == 2:
if c == "R0":
assembly[i] = c
binary[3-i] = "00"
elif c == "R1":
assembly[i] = c
binary[3-i] = "01"
elif c == "R2":
assembly[i] = c
binary[3-i] = "10"
elif c == "R3":
assembly[i] = c
binary[3-i] = "11"
elif len(c) == 4:
assembly[i] = (str(c[3]+c[2]))
assembly[i+1] = (str(c[1]+c[0]))
binary[3-i] = (str(c[2]+c[3]))
binary[3-i-1] = (str(c[0]+c[1]))
break
else:
print("Invalid input")
break
if binary[2] == "INC" or binary[2] == "DEC":
binary[0] = "00"
break
elif binary[3] == "00" and binary[2] != "10" and binary[2] != "11":
current_options = "'R0', 'R1', 'R2', 'R3'"
elif i == 3:
if c == "R0":
assembly[i] = c
binary[3-i] = "00"
elif c == "R1":
assembly[i] = c
binary[3-i] = "01"
elif c == "R2":
assembly[i] = c
binary[3-i] = "10"
elif c == "R3":
assembly[i] = c
binary[3-i] = "11"
elif len(c) == 4:
assembly[i] = c
binary[3-i] = "10"
else: print("Invalid input")
print("assembly inputed:\t", assembly[0]+" "+assembly[1]+" "+assembly[2]+" "+assembly[3]+" ", "\nbinary outputed:\t", binary[0]+binary[1]+binary[2]+binary[3])
BINARY Input
now we need to input the intructions into memory
at the very top of the monstrosity that is my CPU below, there is a place for manual input into memory(look at the image below)
in the manual input there are a few buttons and inputs, the image below describes them